<?php
/**
 * User: b179a9
 * Date: 10/4/11
 * Time: 9:58 PM
 */

class App_Form_Irubric_Form extends Zend_Form
{

    public function init()
    {
        $this->setMethod('post');
        $this->setAction('');

        $id = new Zend_Form_Element_Hidden('id');
        $this->addElement($id);

        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Name:')
                ->setRequired()
                ->addValidators(array(new Zend_Validate_StringLength(1, 255)));
        $this->addElement($name);

        // Родительская рубрика.
        $parent = new Zend_Form_Element_Select('parent');

        $manager = new \Managers\ItemsRubrics();
        $rubrics = $manager->getList(null, true);

        /**
         * Добавляем пустой эелемент (для корневых страниц)
         */
        $parent->addMultiOption('', 'None');
        foreach ($rubrics as $record) {
            $parent->addMultiOption($record['id'], $record['name']);
        }
        $parent->setLabel('Parent rubric if exists:');
        $parent->addErrorMessage('Please, select valid parent rubric.');
        $this->addElement($parent);
        // Конец Родительская страница

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');

        $this->addElement($submit);

        $this->addElement('hash', 'csrf', array(
            'ignore' => true,
        ));
    }

    public function populate($values)
    {
        if (!empty($values['parent'])) {
            $values['parent'] = $values['parent']['id'];
        }
        parent::populate($values);
    }



}
